<?php


namespace App\Http\Controllers\Admin;

use App\Console\Commands\SuggestTimeCommand;
use App\Enums\AppMiniPlateEnum;
use App\Models\AppMiniPlateLog;
use App\Models\Category;
use App\Models\GoodsClass;
use App\Models\MarketingActive;
use App\Services\CommonService;
use Illuminate\Http\Request;
use App\Models\AppMiniPlate;
use Illuminate\Support\Facades\Validator;
use App\Models\AppTypeModel;

class AppMiniPlateController extends BaseController
{

    /**
     * 配置板块
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function configPlate(Request $request)
    {
        $validator = Validator::make($request->input(), [
            'biz_type' => 'required',
            'key' => 'required',
            'range' => 'required',
            'value' => 'sometimes',
            'is_open' => 'sometimes',
        ]);


        if ($validator->fails()) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数有误');
        }


        $params = $request->all();
        $key = $params['key'];
        if (!AppMiniPlateEnum::plateIsValid($key)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '未指定板块或key值错误');
        }

        if (!is_array($params['value'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'value值为数组');
        }

        if ($params['range'] == 1) {
            if (!is_array($params['mini_ids'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'mini_ids数组格式');
            }
            $mini_ids = $params['mini_ids'];
        } else {
            // 0全部1租赁业务2分期业务
            // 范围0全部，1指定
            $where = [];
            if ($params['biz_type'] > 0) {
                $where[] = ['biz_type', $params['biz_type']];
            }
            $mini_ids = AppTypeModel::query()->where($where)->pluck('id');
        }

        $AppMiniPlate = new AppMiniPlate();
        $res = $AppMiniPlate->savePlate($params['key'], $params['value'], $params['is_open'] ? '1' : 0, $mini_ids);
        if ($res) {
            $data=[
                'key'=>$params['key'],
                'description' =>  AppMiniPlateEnum::getPlateName($params['key']),
                'content'=>json_encode($params['value']),
                'is_open'=>$params['is_open']==1 ? '1' : 0,
                'mini_ids'=>$params['range']==1? json_encode($mini_ids) :'',
                'range'=>$params['range']==1? '1' : 0,
                'biz_type'=>$params['biz_type'],
            ];

            AppMiniPlateLog::savePlateLog($data);

            (new SuggestTimeCommand())->handleExec();

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
        }
    }

    public function getPlateLogList(Request $request)
    {
        $params = $request->input();
        $validator = Validator::make($params, [
            'key' => 'required',
        ]);

        if ($validator->fails()) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数有误');
        }

        $data = AppMiniPlateLog::getPlateLogList($params['key']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    public function getPlateLogDetail(Request $request)
    {
        $params = $request->input();
        $validator = Validator::make($params, [
            'id' => 'required',
        ]);

        if ($validator->fails()) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数有误');
        }

        $data = AppMiniPlateLog::getPlateLogDetail($params['id']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    public function getKeyNewPlateDetail(Request $request)
    {
        $params = $request->input();
        $validator = Validator::make($params, [
            'key' => 'required',
            'biz_type' => 'required',
            'range' => 'required',
            'mini_ids' => 'sometimes|required_if:range,1',
        ]);

        if ($validator->fails()) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数有误');
        }

        $params=[
            'key'=>$params['key'],
            'biz_type'=>$params['biz_type'],
            'range'=>$params['range'],
        ];
        if($params['range']==1){
            $params['mini_ids']=json_encode($params['mini_ids']);
        }
        $data = AppMiniPlateLog::getPlateLogCategoryDetail($params);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    public function deletedPlateLog(Request $request)
    {
        $params = $request->input();
        $validator = Validator::make($params, [
            'id' => 'required',
        ]);

        if ($validator->fails()) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数有误');
        }

        $data = AppMiniPlateLog::deletedPlateLog($params['id']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功', $data);
    }

    /**
     *  获取板块列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function getPlateList(Request $request)
    {
        $params = $request->input();
        $validator = Validator::make($params, [
            'key' => 'required',
            'mini_id' => 'required',
        ]);

        if ($validator->fails()) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数有误');
        }

        $data = AppMiniPlate::getMIniPlate($params['mini_id'], $params['key'], $params['cate_id'] ?? 0);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     *  获取小程序列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function getMiniList(Request $request)
    {
        $params = $request->input();
        // 0全部1租赁业务2分期业务
        // 范围0全部，1指定
        $where = [];
        if ($params['biz_type'] > 0) {
            $where[] = ['biz_type', $params['biz_type']];
        }
        $data = AppTypeModel::query()->where($where)->select('app_name', 'id')->get();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', obToArray($data));
    }


    public function pagesRoute()
    {
        $pages[]=['id'=>1,'name'=>'首页','page_url'=>'home','sub'=>[['name'=>'首页','page_url'=>'pages/index/index']]];
        $category = GoodsClass::getAll();
        $category = $category ? $category->toArray() : [];
        foreach ($category as $k=>&$v){
            $v['page_url'] = 'pages/classify/classify?id='.$k;
            if(!empty($v['children'])){
                foreach ($v['children'] as $k1=>&$v1){
                    $v1['page_url'] = 'pagesA/pages/productList/productList?cateId='.$v1['id'];
                }
            }
        }
        $pages[]=['id'=>2,'name'=>'分类','page_url'=>'cate','sub'=>$category];
        $dataActive=MarketingActive::query()
            ->select('id','title as name')
            ->get()->toArray();

        foreach ($dataActive as &$v){
            $v['page_url'] = 'pagesA/pages/promotionPageVariable/promotionPageVariable?id='.$v['id'];
        }
        $pages[]=['id'=>3,'name'=>'活动页','page_url'=>'pagesA/pages/promotionPageVariable/promotionPageVariable','sub'=>$dataActive];


        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取',$pages);
    }
}
